'use strict';

angular.module('hanxinBroker').controller('SubscribeListCtrl', function ($timeout,$scope,$http,ngDialog,growl) {
  var vm = this;
  vm.form = {};
  vm.items = [];
  vm.i = 0 ;
  vm.time = "";
  vm.showSubscribing = true;
  vm.showSubscribed = false;
  var s =undefined;
  vm.noMoreDate = true;

  vm.addSpecialWarnMessage = function() {
    growl.addWarnMessage("未查询出结果",{ttl:"2000"});
  };

  $http.post('broker/api/subscribesList',{"pageNum":vm.i}).success(function(data){
    vm.i++;
    vm.items = _.union(vm.items,data);
    console.log(data);
  }).error(function(){
    alert("未能连接服务器");
  });

  vm.loadMore = function(){
      console.log(vm.i);
      $timeout(getList,2000);
  };

  var getList = function(){
    $http.post('broker/api/subscribesList',{"pageNum":vm.i,"status":s}).success(function(data){
      console.log("successful"+vm.i);
      console.log(data);
      vm.items = _.union(vm.items,data);
      console.log(vm.items.length);
      if(data.length!=0){
        vm.i++;
      }
      $scope.$broadcast('scroll.infiniteScrollComplete');
    }).error(function(data){
      console.log("failed"+data);
      vm.noMoreDate = false;
    });
  };

  vm.dialog = function(){
    ngDialog.openConfirm({
      template: 'queryList',
      controller: 'QueryController'
    }).then(function (value) {
      console.log('Send data to saving database: ', value.mobile);
      console.log('status is ' + !value.status);
      vm.i=0
      if(!value.status){
        $http.post('broker/api/subscribesList',{"mobile":value.mobile,"startTime":value.fromDate,"endTime":value.endDate,"pageNum":vm.i})
          .success(function(data){
            console.log("successful " + data);
            vm.items = data;
            vm.showSubscribed = false;
            vm.showSubscribing = true;
            vm.noMoreDate = true;
            s = undefined;
            if(data.length!=0){
              vm.i++;
            }
          }).error(function(data){
            vm.noMoreDate = false;
            console.log("error..."+data);
          });
      }else{
        console.log("else"+value.status.value);
        if(s != value.status.value){
          vm.i = 1;
          s = value.status.value;
        }
        $http.post('broker/api/subscribesList',{"status":value.status.value,"mobile":value.mobile,"startTime":value.fromDate,"endTime":value.endDate,"pageNum":0})
          .success(function(data){
            console.log("successful " + data);
            vm.items = data;

            if("SUBSCRIBED" == value.status.value){
              vm.showSubscribed = true;
              vm.showSubscribing = false;
            }else{
              vm.showSubscribed = false;
              vm.showSubscribing = true;
            }
            vm.noMoreDate = true;
          }).error(function(data){
            console.log("error..."+data);
            vm.noMoreDate = false;
            vm.addSpecialWarnMessage();
          });
      }

      }, function (reason) {
       console.log('cancel save', reason);
      });
  }
})
  .controller('QueryController',function($scope){
    $scope.queryFrom = {};
    $scope.options = [{label:'预约',value:'SUBSCRIBING'},
      {label:'成功',value:'SUBSCRIBED'},
      {label:'失败',value:'SUBSCRIBE_FAILED'}];
  });
